package com.mypaper.list;

import com.mypaper.elem.TaskAttribute;
import com.mypaper.elem.TaskNode;

public class ListPriority{
	/*
	 * 根据产生的任务节点信息：任务序号及BLevel，构造任务的优先级序列
	 */
	public TaskNode first;
	protected TaskNode last;
	protected int count;	
	
	public ListPriority(){
		first=null;
		last=null;
		count=0;
	}

	public ListPriority(ListPriority otherList){
		first=null;
		last=null;
		count=0;
	}
	
	public void print(){
		TaskNode current;
		current=first;
		while(current!=null){
			System.out.println(current.info);
			current=current.next;
		}
	}
	
	public TaskAttribute popList() {
		TaskAttribute task = new TaskAttribute();
		if(first!=null){
			task=first.info.getCopy();
			first=first.next;
			count--;
			return task;
		}
		else
			return null;
		
	}
	
	public void enDownList(TaskAttribute enItem) {
		TaskNode current;
		TaskNode trailCurrent;
		TaskNode newNode;
		boolean found;
		
		newNode = new TaskNode();
		newNode.info=enItem.getCopy();
		newNode.next=null;
		
		if(first==null){
			first=newNode;
			last=newNode;
			count++;
		}else{
			trailCurrent=first;
			current=first;
			found=false;
			while(current!=null&&!found){
				if(current.info.compareTo(enItem)<0)
					found=true;
				else{
					trailCurrent=current;
					current=current.next;
				}	
			}
			if(current==first){
				newNode.next=first;
				first=newNode;
				count++;
			}else{
				trailCurrent.next=newNode;
				newNode.next=current;
				count++;
			}
		}
	}	
	public void enUpList(TaskAttribute enItem) {
		TaskNode current;
		TaskNode trailCurrent;
		TaskNode newNode;
		boolean found;
		
		newNode = new TaskNode();
		newNode.info=enItem.getCopy();
		newNode.next=null;
		
		if(first==null){
			first=newNode;
			last=newNode;
			count++;
		}else{
			trailCurrent=first;
			current=first;
			found=false;
			while(current!=null&&!found){
				if(current.info.compareTo(enItem)>0)
					found=true;
				else{
					trailCurrent=current;
					current=current.next;
				}	
			}
			if(current==first){
				newNode.next=first;
				first=newNode;
				count++;
			}else{
				trailCurrent.next=newNode;
				newNode.next=current;
				count++;
			}
		}
	}	
	
	public void enListLast(TaskAttribute enItem) {
		TaskNode newNode;	
		newNode = new TaskNode();
		newNode.info=enItem.getCopy();
		newNode.next=null;
		
		if(first==null){
			first=newNode;
			last=newNode;
			count++;
		}else{
			last.next=newNode;
			last=newNode;
		}
	}	
	
	public void enListFirst(TaskAttribute enItem) {
		TaskNode newNode;	
		newNode = new TaskNode();
		newNode.info=enItem.getCopy();
		newNode.next=null;
		
		if(first==null){
			first=newNode;
			last=newNode;
			count++;
		}else{
			newNode.next=first;
			first=newNode;
		}
	}	
}
